package com.ymttest.database.mongo.config;

import static com.ymttest.database.mongo.config.SchemaField.ORDER;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.ymttest.database.model.mongo.query.Order;
import com.ymttest.database.mongo.converter.YmtConverters;
import com.ymttest.utils.EnvSetup;

public class MongoOrderConfig extends AbstractMongoConfiguration {
	private MongoClient mongo;

	@Override
	protected String getDatabaseName() {
		return ORDER;
	}

	@Override
	public MongoClient mongo() throws Exception {
		System.setProperty("DEBUG.MONGO", "true");
		System.setProperty("DB.TRACE", "true");
		MongoClientURI uri = new MongoClientURI(
				EnvSetup.getSystemUrl("set5_mongo_order_uri"));
		this.mongo = new MongoClient(uri);
		return this.mongo;
	}

	public void close() {
		if (this.mongo != null) {
			this.mongo.close();
		}
	}

	@Bean
	@Override
	public MongoDbFactory mongoDbFactory() throws Exception {
		return new SimpleMongoDbFactory(mongo(), ORDER);
	}

	@Bean
	@Override
	public MongoTemplate mongoTemplate() throws Exception {
		return new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
	}

	@Bean
	@Override
	public MappingMongoConverter mappingMongoConverter() throws Exception {
		MappingMongoConverter mmc = super.mappingMongoConverter();
		List<Converter<?, ?>> list = YmtConverters.getConvertersToRegister();
		CustomConversions customConversions = new CustomConversions(list);
		mmc.setCustomConversions(customConversions);
		return mmc;
	}

	public static void main(String[] args) throws Exception {
		// MongoOrderConfig config =
		// SpringContextHolder.getBean(MongoOrderConfig.class);
		// MongoTemplate template = config.mongoTemplate();

		MongoTemplate template = new MongoOrderConfig().mongoTemplate();
		Criteria criteria = Criteria.where(OrderField.ID).is(112517508);
		Query query = new Query(criteria);
		Order order = template.findOne(query, Order.class, "Order");
		// System.out.println(order.getTimeMongoAdd());
		System.out.println(order.getAddTime());
		// System.out.println(order.getMainOrderId());
		// System.out.println(order.getAutoCancelHour());
		// System.out.println(order.getProducts().get(0).isPspProduct());
		// Update update = new Update();
		// update.set("prds.0.psp", false);
		// // template.updateFirst(query, update, "Order");
		//
		// Integer a = new Integer(2);
		// Integer b = new Integer(2);
		// System.out.println(a==b);
		// System.out.println(a.equals(b));
		Criteria c = Criteria.where("prds")
				.elemMatch(
						Criteria.where("prdId")
								.is("ac3a5721-e884-4ed4-9b57-ab8365908syf")
								.andOperator(
										Criteria.where("title").is(
												"修改修改仿，666667")));
		List<Order> orders = template.find(new Query(c), Order.class, "Order");
		System.out.println(orders.size());
	}

}
